#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

bool IsLetter(char c);

bool IsLetter(char c){
  /*Check for a match of ASCII capital and lower case letters.*/
  if (c >= 65 && c <= 90 || (c >= 97 && c <= 122)){
    return true;
  }
  return false;
}

int main(int argc, char* argv[]) {
  /*Open file and check existance.*/
  FILE * fp = fopen(argv[1],"r");
  if (fp == NULL){
    printf("Unable to load file %s\n", argv[1]);
    exit(1);
  }
  /*Store file size and allocate memory for the file data.*/
  fseek(fp, 0L, SEEK_END);
  long int fsize = ftell(fp);
  fseek(fp, 0L, SEEK_SET);
  char * Buffer = malloc(fsize);
  
  if (Buffer){
    fread(Buffer,1,fsize,fp);
  }
  fclose(fp);
  
  int word_count = 0;
  
  //Check for a word at the very start of the file
  if (IsLetter(Buffer[0])){
    word_count++;
  }
  
  for (int i = 0; i < fsize; i++){
    /*Under these conditions a new word has been found.*/
    if (!IsLetter(Buffer[i]) && IsLetter(Buffer[i+1])){
      word_count++;
    }
  }
  printf("Words: %d\n", word_count);
  free(Buffer);
  return 0;
}
